﻿using UnityEngine;
using System.Threading.Tasks;
using System.Collections;

public class SyncCallAsyncWithCoroutine : MonoBehaviour
{
    private void Start()
    {
        // 同步方法启动协程调用异步方法
        StartCoroutine(CallAsyncMethodWithCoroutine());
    }

    private IEnumerator CallAsyncMethodWithCoroutine()
    {
        // 启动异步任务
        Task<string> task = AsyncMethod();
        // 等待异步任务完成
        while (!task.IsCompleted)
        {
            yield return null;
        }
        // 获取任务结果
        string result = task.Result;
        Debug.Log("异步任务结果: " + result);
    }
    

    private async Task<string> AsyncMethod()
    {
        // 模拟耗时操作
        await Task.Delay(2000);
        return "异步任务完成";
    }

    void Update()
    {
        Debug.Log("Update" );
    }
}